package com.shengyijie.adapter.viewadapter;

import java.util.List;

import com.shengyijie.activity.R;
import com.shengyijie.activity.tab.HomeActivity;
import com.shengyijie.adapter.asyncimageadapter.GalleryImageLoader;
import com.shengyijie.adapter.asyncimageadapter.GalleryImageLoader.ImageCallback;
import com.shengyijie.adapter.view.GalleryFlow;
import com.shengyijie.adapter.view.HomeGalleryFlow;
import com.shengyijie.adapter.viewcache.ImageViewCache;
import com.shengyijie.context.ContextApplication;
import com.shengyijie.model.object.baseobject.AdvertPic;
import com.shengyijie.util.ImageUtility;
 

import android.app.Activity;
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.Drawable; 
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter; 
import android.widget.ImageView;  

public class HomeGalleryAdapter extends ArrayAdapter<AdvertPic> {
 
	private Context mContext; 
	
	private HomeGalleryFlow galleryFlow;
	private GalleryImageLoader asyncImageLoader;   
	public  ContextApplication app;
	public int size;
	public HomeGalleryAdapter(Activity activity,Context c, List<AdvertPic> newsList,HomeGalleryFlow listView) {
		 super(activity, 0, newsList);
		 asyncImageLoader = new GalleryImageLoader(); 
		 mContext = c;
		 this.galleryFlow=listView; 
		 app=(ContextApplication) activity.getApplication();
		 size=newsList.size();
	}
	
	public int getCount()  
    {  
        return Integer.MAX_VALUE;  
    }  
	
	   
	 

	public Drawable createReflectedImages(Drawable src) {
		final int reflectionGap = 0; 
			Bitmap originalImage =ImageUtility.DrawableToBitmap(src);
			int width = originalImage.getWidth();
			int height = originalImage.getHeight();

			Matrix matrix = new Matrix();
//			matrix.setRotate(30);
			matrix.preScale(1, -1);

			Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0,
					height /6*5, width, height /6, matrix, false);

			Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
					(height + height/4), Config.ARGB_8888);

			Canvas canvas = new Canvas(bitmapWithReflection);
			canvas.drawBitmap(originalImage, 0, 0, null);

			Paint deafaultPaint = new Paint(); 
			// deafaultPaint.setAntiAlias(true);
			canvas.drawRect(0, height, width, height + reflectionGap,
					deafaultPaint);

			canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

			Paint paint = new Paint(); 
			 paint.setAntiAlias(true);
			LinearGradient shader = new LinearGradient(0,
					originalImage.getHeight(), 0,
					bitmapWithReflection.getHeight() + reflectionGap,
					0x70ffffff, 0x00ffffff, TileMode.CLAMP);

			paint.setShader(shader);

			paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
			canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
					+ reflectionGap, paint);
 
		     return ImageUtility.BitmapToDrawable(bitmapWithReflection);
	}
 

	public View getView(int position, View convertView, ViewGroup parent) { 
		ImageViewCache viewCache;  
		try{ 
				if(convertView == null){
			         //imageView = new ImageView(mContext);  
					convertView = new ImageView(mContext); 
					convertView.setLayoutParams(new GalleryFlow.LayoutParams(300,550));	
					viewCache = new ImageViewCache((ImageView)convertView);
					convertView.setTag(viewCache);
				} else {
					viewCache = (ImageViewCache) convertView.getTag();
				}
				 
				
				try{
					ImageView imageView = viewCache.getImageView();
//					Animation  animation1 = AnimationUtils.loadAnimation(mContext, R.anim.htranslate);
//					convertView.startAnimation(animation1);
					
					
					String imageUrl = getItem(position%size).getPic_url();
					 
					imageView.setTag(imageUrl);
					Drawable cachedImage = null;
				 
					cachedImage = asyncImageLoader.loadDrawable(imageUrl, new ImageCallback() {
						public void imageLoaded(Drawable imageDrawable, String imageUrl) {
							ImageView imageViewByTag = (ImageView) galleryFlow.findViewWithTag(imageUrl);
							if (imageViewByTag != null) {
								imageViewByTag.setImageDrawable(createReflectedImages(imageDrawable)); 
							}
						}
					});
					  
					if (cachedImage == null) {
						Drawable d = mContext.getResources().getDrawable(R.drawable.show);
						imageView.setImageDrawable(createReflectedImages(d));
					}else{
						imageView.setImageDrawable(createReflectedImages(cachedImage));
						cachedImage=null;
					}
					 
				}catch(Exception e){} 
		  }catch(Exception e){}
		return convertView;
	}

	public float getScale(boolean focused, int offset) {
		return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));
	}

}
